package com.simple.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.simple.entity.MailBean;
import com.simple.entity.SysUser;
import com.simple.repository.SysUserRespository;
import com.simple.service.SendMailService;
import com.simple.service.SysUserService;
import com.simple.utils.EncryptionUtils;
import com.simple.utils.RandomUtils;
import com.simple.utils.SysConfig;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserRespository sysUserRespository;
    
    @Autowired
    private SendMailService sendMailService;

    @Value("${upload.myLocation}")
    private String path;

    @Value("${upload.url}")
    private String uploadUrl;

    @Override
    public SysUser save(SysUser sysUser) {
        String password=sysUser.getPassword();//获取密码
        sysUser.setPassword(EncryptionUtils.shiroMd5(password));//加密密码
        sysUser.setHeadPortrait("https://daliang9527-1257251098.cos.ap-beijing.myqcloud.com/images/20210907155302.jpg");
        sysUser.setStatus(0);
        sysUser.setCreateDate(new Date());
        sysUserRespository.save(sysUser);
        return sysUser;
    }

    @Override
    public SysUser selectEmail(String email) {
        SysUser sysUser=sysUserRespository.selectEmail(email);
        return sysUser;
    }

    @Override
    public SysUser selectUserName(String username) {
        SysUser sysUser=sysUserRespository.selectUserName(username);
        return sysUser;
    }

    public SysUser saveAndFlush(SysUser sysUser) {
        sysUserRespository.saveAndFlush(sysUser);
        return sysUser;
    }

    @Override
    public List<SysUser> findAll() {
        // TODO Auto-generated method stub
        return sysUserRespository.findAll();
    }
    @Override
    public void relationRole(Integer id,Integer roleId) {
        sysUserRespository.relationRole(id,roleId);
    }

    @Override
    public void setOnlineStatus(String username, Integer status) {
        // TODO Auto-generated method stub
        sysUserRespository.setOnlineStatus(username, status);
    }

//    @Override
//    public List<SysUserVo> findAllOnline(Integer id) {
//        // TODO Auto-generated method stub
//        return sysUserRespository.findAllOnline(id);
//    }

    @Override
    public Integer findAllOnlineCount(String username) {
        // TODO Auto-generated method stub
        return sysUserRespository.findAllOnlineCount(username);
    }
    @Transactional
    @Override
    public void activationUser(Integer status,Integer userId) {
        sysUserRespository.activationUser(status,userId);
    }

    @Transactional
    @Override
    public void headPortraitUpload(MultipartFile file, Integer id) {
        String fileName=file.getOriginalFilename();
        String suffixName=fileName.substring(fileName.lastIndexOf(".")+1);
        String newFileName= RandomUtils.getId()+"."+suffixName;
        String url=uploadUrl+newFileName;
        File newFile=new File(path,newFileName);
        try {
            file.transferTo(newFile);
            sysUserRespository.headPortraitUpload(url, id);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
	@Override
	public void deleteUserById(Integer id) {
    	sysUserRespository.deleteUserById(id);
	}

	@Override
	public SysUser findByUserId(Integer id) {
		return sysUserRespository.findByUserId(id);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void modifyPassword(String password) {
		String encryptionPwd=EncryptionUtils.shiroMd5(password);
		Integer id=SysConfig.getUser().getId();
		sysUserRespository.modifyPassword(encryptionPwd, id);
	}

	@Override
	public void sendMail(SysUser sysUser,HttpServletRequest request) {
		MailBean mailBean=new MailBean();
		mailBean.setReceiver(sysUser.getEmail());
		mailBean.setSubject("后台账号激活");
		Map<String, Object> map=new HashMap<String, Object>();
		String encriotionMail=EncryptionUtils.encryptBase64(sysUser.getEmail(),"daliang9527");
		String activationUrl="http://"+request.getServerName()+":"+request.getServerPort()+"/activation.html?sign="+encriotionMail;
		map.put("url", activationUrl);
		mailBean.setMap(map);
		sendMailService.sendMail(mailBean,false);
	}
}

